
#include "QRenderIO.h"

QRenderIO::QRenderIO()
{

}

QRenderIO::~QRenderIO()
{

}

unsigned char QRenderIO::getFileContent(std::string fileName, std::string &content)
{
    std::ifstream file(fileName.c_str());
    if (!file)
    {
        std::cerr << " > ERROR: unable to open input file: \"" << fileName << "\"." <<  std::endl;
        return GL_FALSE;
    }
    
    file.seekg(0, std::ios::end);
    int length = file.tellg();
    file.seekg(0, std::ios::beg);
    
    content.reserve(length);
    content.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
    file.close();
    
    return GL_TRUE;
}

unsigned char QRenderIO::getFileData(std::string fileName, void *data, unsigned int size)
{
    std::ifstream file(fileName.c_str(), std::ios::binary);
    if (!file)
    {
        std::cerr << " > ERROR: unable to open input file: \"" << fileName << "\"." <<  std::endl;
        return GL_FALSE;
    }
	
    file.read((char *)data, size);
    if (file.gcount() != size)
    {
        std::cerr << " > ERROR: reading data failed." << std::endl;
        return GL_FALSE;
    }
    file.close();
    return GL_TRUE;
}

unsigned char saveFileData(std::string fileName, void *data, unsigned int size)
{
    std::ofstream file(fileName.c_str(), std::ios::binary);
    if (!file)
    {
        std::cerr << " > ERROR: unable to open output file: \"" << fileName << "\"." <<  std::endl;
        return GL_FALSE;
    }

    file.write((char *)data, size);
    if (file.fail())
    {
        std::cerr << " > ERROR: writing data failed." << std::endl;
        return GL_FALSE;
    }
    file.close();

    return GL_TRUE; 
}